<!--
 * @Author: [kangjie]
 * @Date: 2022-04-28 09:20:08
 * @LastEditors: [kangjie]
 * @LastEditTime: 2022-04-28 09:33:59
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>极简版的双向数据绑定</title>
</head>
<body>
  <p id="p_id">极简版的双向数据绑定</p>
  <input type="text" id="p_id">
  <script>
    let obj={
      name:'zhangsan'
    };
    Object.defineProperty(obj,'name',{
      get(){
        console.log('有人获取了值');
      },
      set(newValue){
        document.getElementById('p_id').innerHTML=newValue;
        document.getElementById('p_id').value=newValue;
      }
    });
    // obj.name='王五';
    document.addEventListener('keyup',function(e){
      obj.name=e.target.value
    })
  </script>
</body>
</html>